package com.hinnotek.warehouse.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.databinding.ActivityLoginNewBinding
import com.hinnotek.warehouse.viewmodel.LoginViewModel
import com.lee.common.base.BaseActivity
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.*
import dagger.hilt.android.AndroidEntryPoint

/**
 * 登录Activity
 */
@Route(path = ACTIVITY_LOGIN_NEW)
@AndroidEntryPoint
class LoginNewActivity : BaseActivity<ActivityLoginNewBinding>() {

    private val viewModel by viewModels<LoginViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        /* window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
         actionBar?.hide()*/

        /*StatusBarUtils.fullScreen(this)
        *//*  .addItemView(groupListView.createItemView("设置状态栏黑色字体与图标"), v -> StatusBarUtils.setStatusBarLightMode(getActivity()))
                .addItemView(groupListView.createItemView("设置状态栏白色字体与图标"), v -> StatusBarUtils.setStatusBarDarkMode(getActivity()))*//*
        //设置沉浸式状态栏
        StatusBarUtils.setStatusBarDarkMode(this)
        */

        //StatusBarUtils.translucent(this)

        setContentView(R.layout.activity_login_new)
        setTitleBarGone()
        bindingView.loginModel = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))

        if (SharedPreUtils.getBoolean("isLogin", false)) {
            Skipping.start(ACTIVITY_MAIN_NEW)
            finish()
        }
    }

    override fun initVew() {
        bindingView.login.setOnClickListener {
            verifyPhone()
            viewModel.loadData()
        }
    }

    override fun setData() {

        viewModel.loginPage.observe(this, Observer { data ->
            if (data != null) {
                accessToken = data.token
                val sharedPreferences =
                    getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)

                val editor = sharedPreferences.edit()
                editor.putBoolean("isLogin", true) //登录状态
                editor.putInt("userId", data.userId) //用户ID
                editor.putString("userName", data.userName) //账号
                val userPassword = viewModel.userPass.get()
                editor.putString(
                    "userPassword", userPassword
                )
                editor.putString("createTime", data.createTime) //注册时间
                editor.putInt("roleId", data.roleId) //角色ID
                editor.putString("personName", data.personName) //角色姓名
                editor.putInt("orgId", data.orgId) //组织ID
                editor.putString("orgName", data.orgName) //组织名称
                editor.putString("token", data.token) //token
                editor.putInt(isAmoebaOrCompany, data.typeId) //角色ID
                for (dataBean in data.sysRolePriList) {
                    when (dataBean.priId) {
                        2 -> editor.putBoolean(isCompanyBusiness, true) //公司商务
                        3 -> editor.putBoolean(isCompanyFinance, true) //公司财务
                        4 -> editor.putBoolean(isPhoneNumPay, true) //手机号缴费
                        5, 9 -> editor.putBoolean(isNoticeApprove, true) //公司消息审批
                        6 -> editor.putBoolean(isAmoebaSell, true) //阿米巴销售
                        7 -> editor.putBoolean(isAmoebaTechnology, true) //阿米巴技术
                        8 -> editor.putBoolean(isAmoebaRD, true) //阿米巴研发
                        10 -> editor.putBoolean(isAmoebaStatistics, true) //阿米巴统计
                        15 -> editor.putBoolean(isAmoebaWareHouse, true) //阿米巴统计
                    }
                }
                editor.apply()
                ToastUtils.showShort("登录成功")
                Skipping.start(ACTIVITY_MAIN_NEW)
                loginSuccess()
            }else{

            }
        })
    }

    private fun verifyPhone(): Boolean {
        if (TextUtils.isEmpty(viewModel.userName.get())) {
            ToastUtils.showLong("请输入用户名")
            return false
        }
        if (TextUtils.isEmpty(viewModel.userPass.get())) {
            ToastUtils.showLong("请输入密码")
            return false
        }
        return true
    }

    //登录成功
    private fun loginSuccess() {
        //MyActivityManager.instance.holdBackActivity(MainActivity::class.java)
        finish()
    }


}